package com.wf.course.web.controller.member;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.utils.dateUtil.DateUtil;
import com.utils.page.PageBean;
import com.wf.course.model.member.CoinLog;
import com.wf.course.model.member.Member;
import com.wf.course.service.ICoinLogService;
import com.wf.course.service.IMemberService;
import com.wf.course.web.Interceptor.LogInfo;
/**
 * 
 * @content 用户Controller
 */
@RequestMapping("coinLog")
@Controller
public class CoinLogController {

	@Autowired
	private ICoinLogService coinService;
	
	@Autowired
	private IMemberService memberService;

	/**
	 *  列表
	 * @param pageNo
	 * @param pageSize
	 * @param userName
	 * @param flowFlag
	 * @param resource
	 * @param status
	 * @param model
	 * @return
	 */
	@RequestMapping("coinLogList")
	public String chapterList(@RequestParam(value = "pageNo", required = false) Integer pageNo,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			@RequestParam(value = "telephone", required = false) String telephone, 
			@RequestParam(value = "flowFlag", required = false) String flowFlag, 
			@RequestParam(value = "resource", required = false) String resource, 
			@RequestParam(value = "status", required = false) String status, 
			@RequestParam(value = "deleteFlag", required = false) String deleteFlag, 
			
			Model model) {

		// 组装一个查询条件的map
		Map<String, Object> cons = new HashMap<String, Object>();
		cons.put("telephone", telephone);
		cons.put("flowFlag", flowFlag);
		cons.put("resource", resource);
		cons.put("status", status);
		cons.put("deleteFlag", deleteFlag);
		
		PageBean<CoinLog> pageBean = new PageBean<CoinLog>(pageNo, pageSize, null, cons);
		pageBean.setUrl("coinLog/coinLogList");
		this.coinService.indexInfo(pageBean);
		 
		model.addAttribute("page", pageBean);
		return "member/coinLogList";
	}

	/**
	 *  修改和新增页面数据初始化
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("pageLoad/{id}")
	public String downLoadById(@PathVariable("id") Integer id, Model model) {
		 
		if (id != null && id != 0) {
			CoinLog coinLog = coinService.queryCoinLogById(id);
			model.addAttribute("coinLog", coinLog);
			model.addAttribute("type", "upd");
			return "member/coinLogEdit";
		}else{
			model.addAttribute("type", "add");
			return "member/coinLogAdd";
		}
		
		
	}

	/**
	 *  修改
	 * @param member
	 * @param request
	 * @return
	 */
	@LogInfo("修改铜钱记录信息")
	@RequestMapping(value = "coinLogEdit" , produces = "text/html;charset=UTF-8")
	public String memberEdit(@ModelAttribute CoinLog coinLog,HttpServletRequest request) {
		
		coinService.saveOrUpdateCoinLog(coinLog);
		
		return "redirect:/coinLog/coinLogList";
	}

	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@LogInfo("删除铜钱记录信息")
	@RequestMapping(value = "remove/{id}")
	public String removemember(@PathVariable("id") Integer id) {
		
		coinService.deleteById(id);
		
		return "redirect:/coinLog/coinLogList";
	}
	
	
	/**
	 *  修改
	 * @param member
	 * @param request
	 * @return
	 */
	@LogInfo("新增铜钱记录信息")
	@RequestMapping(value = "coinLogAdd" , produces = "text/html;charset=UTF-8")
	public String coinLogAdd(@ModelAttribute CoinLog coinLog,HttpServletRequest request) {

		if(coinLog !=null && !"".equals(coinLog.getTelephone())){
			List<Member> mbs = memberService.queryMembersByTelephone(coinLog.getTelephone());
			if(mbs !=null && mbs.size()>0){
				coinLog.setUserId(mbs.get(0).getUserId());
			}
		}
		String time = DateUtil.getDateString(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
		
		coinLog.setCreateTime(time);
		
		coinLog.setStatus(2);
		coinLog.setDeleteFlag(0);
		
		coinService.saveOrUpdateCoinLog(coinLog);
		
		return "redirect:/coinLog/coinLogList";
	}

}
